package cc.fs.video.myvideoproject.ui

import android.app.Fragment
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.Toolbar
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import cc.fs.video.myvideoproject.R
import cc.fs.video.myvideoproject.ui.fragment.FavFileFragment
import cc.fs.video.myvideoproject.ui.fragment.VideoFragment
import cc.fs.video.myvideoproject.utils.bindView

class MainActivity : BaseActivity() {

    val flagSelectFile:Int = 1001

    val toolbar: Toolbar by bindView(R.id.toolbar)
    var currentPage:Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val toolbar = findViewById(R.id.toolbar) as Toolbar
//        setSupportActionBar(toolbar)
        setUpFragment()

        runDelay({
            Toast.makeText(applicationContext," 执行了 ",Toast.LENGTH_SHORT).show();
        },5000);

    }

    var videoFragment: VideoFragment ?= null
    var favFileFragment: FavFileFragment ?= null

    private fun setUpFragment(){
        val fragmentTransaction = fragmentManager.beginTransaction()
        videoFragment = initFragment(R.id.container,"video",VideoFragment::class.java)
        favFileFragment = initFragment(R.id.container,"favFile",FavFileFragment::class.java)
       onPageSelector(0)
    }

    public fun onPageSelector(pos:Int){
        val fragmentTransaction = fragmentManager.beginTransaction()
        if(pos == 0){
            currentPage = 0;
            fragmentTransaction.show(favFileFragment).hide(videoFragment).commit()

        }else{
            currentPage = 1;
            fragmentTransaction.show(videoFragment).hide(favFileFragment).commit();
        }
    }

    public fun selectFile(path:String){
        onPageSelector(1)
        videoFragment?.setFile(path)
    }

    private fun <T: Fragment> initFragment(containerId: Int, tag: String, fragmentClass: Class<T>): T? {
        val fragmentTransaction = fragmentManager.beginTransaction()

        var fragment = fragmentManager.findFragmentByTag(tag) as T?
        if (fragment?.isDetached ?: true) {
            fragment = fragmentClass.newInstance()
            fragmentTransaction.add(containerId, fragment, tag).commit()
        }

        return fragment
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.itemId

        if (id == R.id.action_select_file) {
            FileActivity.start(this,flagSelectFile)
            return true
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onBackPressed() {
        if(currentPage == 0){
            super.onBackPressed()
        }else{
            onPageSelector(0)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        when(requestCode){
            flagSelectFile -> {
            }
        }
    }
}
